<template>
  <div class="server-connection">
    <div class="form-container">
      <h2 class="title">服务器连接</h2>
      
      <div class="input-row">
        <div class="form-group host">
          <label>主机名/IP地址</label>
          <input 
            v-model="serverInfo.host" 
            type="text" 
            placeholder="例如：192.168.1.100"
            class="input-field"
          >
        </div>
        
        <div class="form-group port">
          <label>SSH端口</label>
          <input 
            v-model="serverInfo.port" 
            type="text" 
            placeholder="22"
            class="input-field"
          >
        </div>
      </div>

      <div class="input-row">
        <div class="form-group">
          <label>用户名</label>
          <input 
            v-model="serverInfo.username" 
            type="text" 
            placeholder="请输入用户名"
            class="input-field"
          >
        </div>

        <div class="form-group">
          <label>密码</label>
          <input 
            v-model="serverInfo.password" 
            type="password" 
            placeholder="请输入密码"
            class="input-field"
          >
        </div>
      </div>

      <div class="form-group checkbox">
        <label class="checkbox-label">
          <input 
            v-model="serverInfo.disablePassword" 
            type="checkbox"
          >
          <span>配置完成后禁用密码登录（推荐）</span>
        </label>
      </div>

      <div class="button-container">
        <button 
          @click="handleConfigureSSH" 
          class="submit-button"
          :disabled="isConfiguring"
        >
          <span class="button-icon">🔒</span>
          {{ isConfiguring ? '正在配置...' : '配置SSH密钥' }}
        </button>
      </div>

      <div v-if="message" :class="['message', messageType]">
        <span class="message-icon">
          {{ messageType === 'success' ? '✓' : messageType === 'error' ? '✕' : 'ℹ' }}
        </span>
        {{ message }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ConfigureSSH } from '../../wailsjs/go/main/App'

const serverInfo = reactive({
  host: '',
  port: '22',
  username: '',
  password: '',
  disablePassword: true
})

const isConfiguring = ref(false)
const message = ref('')
const messageType = ref('')

const handleConfigureSSH = async () => {
  if (!serverInfo.host || !serverInfo.username || !serverInfo.password) {
    message.value = '请填写完整的服务器信息'
    messageType.value = 'error'
    return
  }

  try {
    isConfiguring.value = true
    message.value = '正在配置服务器...'
    messageType.value = 'info'

    const result = await ConfigureSSH(
      serverInfo.host,
      serverInfo.port,
      serverInfo.username,
      serverInfo.password,
      serverInfo.disablePassword
    )

    if (result.success) {
      message.value = result.message
      messageType.value = 'success'
    } else {
      message.value = result.message
      messageType.value = 'error'
    }
  } catch (error) {
    message.value = `配置失败: ${error.message}`
    messageType.value = 'error'
  } finally {
    isConfiguring.value = false
  }
}
</script>

<style scoped>
.server-connection {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.form-container {
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 40px;
  font-size: 24px;
  font-weight: 600;
}

.input-row {
  display: flex;
  gap: 20px;
  margin-bottom: 24px;
}

.form-group {
  flex: 1;
}

.form-group.host {
  flex: 2;
}

.form-group.port {
  flex: 1;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-weight: 500;
  font-size: 14px;
}

.input-field {
  width: 100%;
  padding: 12px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  background-color: #f8fafc;
}

.input-field:focus {
  border-color: #3b82f6;
  background-color: white;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  outline: none;
}

.checkbox {
  margin: 24px 0;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.checkbox-label input[type="checkbox"] {
  margin-right: 10px;
  width: 16px;
  height: 16px;
}

.checkbox-label span {
  color: #2c3e50;
  font-size: 14px;
}

.button-container {
  text-align: center;
  margin-top: 32px;
}

.submit-button {
  background-color: #3b82f6;
  color: white;
  border: none;
  padding: 14px 32px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 200px;
}

.submit-button:hover {
  background-color: #2563eb;
  transform: translateY(-1px);
}

.submit-button:disabled {
  background-color: #94a3b8;
  cursor: not-allowed;
  transform: none;
}

.button-icon {
  margin-right: 8px;
  font-size: 18px;
}

.message {
  margin-top: 24px;
  padding: 16px;
  border-radius: 8px;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.message-icon {
  margin-right: 12px;
  font-size: 18px;
}

.message.success {
  background-color: #f0fdf4;
  color: #166534;
  border: 1px solid #dcfce7;
}

.message.error {
  background-color: #fef2f2;
  color: #991b1b;
  border: 1px solid #fee2e2;
}

.message.info {
  background-color: #f0f9ff;
  color: #075985;
  border: 1px solid #e0f2fe;
}

@media (max-width: 640px) {
  .input-row {
    flex-direction: column;
    gap: 16px;
  }
  
  .form-container {
    padding: 24px;
  }
  
  .submit-button {
    width: 100%;
  }
}
</style> 